package com.aop;

import com.util.TransactionUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;

@Aspect
@Component
@EnableAspectJAutoProxy
@Scope("prototype")
public class TransactionAop{

    @Autowired
    TransactionUtil transactionUtil;

    @Pointcut("execution(* com.service..*.*(..))")
    public void transactionAop(){

    }

    @Around("transactionAop()")
    public Integer doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        TransactionStatus transactionStatus = transactionUtil.begin();
        try{
            joinPoint.proceed();
            transactionUtil.commit(transactionStatus);
            return 1;
        }catch (Exception e){
            transactionUtil.rollback(transactionStatus);
            return 0;
        }
    }
}
